package com.mike.bdb.util;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

/**
 * 图片资源管理
 * @author <a href="zhaoming23@gmail.com"> Mike </a>
 * @version 1.0
 * 2011-4-17 下午05:45:08
 */
public class ImageManager {

	private static final String ICONS_PATH = "/icons/";
	public static final String LOGO_MING = ICONS_PATH + "ming.png";
	public static final String ICON_EXIT = ICONS_PATH + "door_out.png";
	public static final String ICON_NEW = ICONS_PATH + "new.png";
	public static final String ICON_BOOK_OPEN = ICONS_PATH + "book_open.png";
	public static final String ICON_BOOK = ICONS_PATH + "book.png";
	public static final String ICON_BOOK_ADD = ICONS_PATH + "book_add.png";
	public static final String ICON_DATABASE = ICONS_PATH + "database.png";
	public static final String ICON_ADD = ICONS_PATH + "add.png";
	public static final String ICON_CANCEL = ICONS_PATH + "cancel.png";
	public static final String ICON_HELP = ICONS_PATH + "help.png";
	public static final String ICON_COMMENT_EDIT = ICONS_PATH + "comment_edit.png";
	public static final String ICON_PLUGIN = ICONS_PATH + "plugin.png";
	public static final String ICON_INFORMATION = ICONS_PATH + "information.png";
	public static final String ICON_ATTACH = ICONS_PATH + "attach.png";
	public static final String ICON_CUP = ICONS_PATH + "cup.png";
	public static final String ICON_BELL = ICONS_PATH + "bell.png";
	public static final String ICON_TAG_GREEN = ICONS_PATH + "tag_green.png";
	public static final String ICON_RAINBOW = ICONS_PATH + "rainbow.png";
	public static final String ICON_PALETTE = ICONS_PATH + "palette.png";
	public static final String ICON_COLOR_WHEEL = ICONS_PATH + "color_wheel.png";
	public static final String ICON_FOLDER = ICONS_PATH + "folder.png";
	public static final String ICON_ACCEPT = ICONS_PATH + "accept.png";
	public static final String ICON_LINK = ICONS_PATH + "link.png";
	public static final String ICON_LINK_BREAK = ICONS_PATH + "link_break.png";
	public static final String ICON_DATABASE_GEAR = ICONS_PATH + "database_gear.png";
	public static final String ICON_DATABASE_GO = ICONS_PATH + "database_go.png";
	public static final String ICON_BUG = ICONS_PATH + "bug.png";
	public static final String ICON_KEY = ICONS_PATH + "key.png";
	public static final String ICON_BULLET_KEY = ICONS_PATH + "bullet_key.png";
	public static final String ICON_BULLET_KEY2 = ICONS_PATH + "bullet_key2.png";
	public static final String ICON_KEY_GO = ICONS_PATH + "key_go.png";
	public static final String ICON_TABLE_EDIT = ICONS_PATH + "table_edit.png";
	public static final String ICON_DATABASE_REFRESH = ICONS_PATH + "database_refresh.png";
	public static final String ICON_FIND = ICONS_PATH + "find.png";
	public static final String ICON_FIND_BY_KEY = ICONS_PATH + "find_by_key.png";
	public static final String ICON_FIND_KEY2 = ICONS_PATH + "find_key2.png";
	public static final String ICON_MIKE = ICONS_PATH + "mike.jpg";
	private static ImageRegistry imageRegistry;

	private static final void declareImages() {
		registerImage(LOGO_MING, LOGO_MING);
		registerImage(ICON_EXIT, ICON_EXIT);
		registerImage(ICON_BOOK, ICON_BOOK);
		registerImage(ICON_BOOK_OPEN, ICON_BOOK_OPEN);
		registerImage(ICON_BOOK_ADD, ICON_BOOK_ADD);
		registerImage(ICON_DATABASE, ICON_DATABASE);
		registerImage(ICON_NEW, ICON_NEW);
		registerImage(ICON_ADD, ICON_ADD);
		registerImage(ICON_CANCEL, ICON_CANCEL);
		registerImage(ICON_HELP, ICON_HELP);
		registerImage(ICON_COMMENT_EDIT, ICON_COMMENT_EDIT);
		registerImage(ICON_PLUGIN, ICON_PLUGIN);
		registerImage(ICON_INFORMATION, ICON_INFORMATION);
		registerImage(ICON_ATTACH, ICON_ATTACH);
		registerImage(ICON_CUP, ICON_CUP);
		registerImage(ICON_BELL, ICON_BELL);
		registerImage(ICON_TAG_GREEN, ICON_TAG_GREEN);
		registerImage(ICON_BULLET_KEY, ICON_BULLET_KEY);
		registerImage(ICON_BULLET_KEY2, ICON_BULLET_KEY2);
		registerImage(ICON_KEY, ICON_KEY);
		registerImage(ICON_KEY_GO, ICON_KEY_GO);
		registerImage(ICON_RAINBOW, ICON_RAINBOW);
		registerImage(ICON_PALETTE, ICON_PALETTE);
		registerImage(ICON_COLOR_WHEEL, ICON_COLOR_WHEEL);
		registerImage(ICON_FOLDER, ICON_FOLDER);
		registerImage(ICON_ACCEPT, ICON_ACCEPT);
		registerImage(ICON_LINK, ICON_LINK);
		registerImage(ICON_LINK_BREAK, ICON_LINK_BREAK);
		registerImage(ICON_DATABASE_GEAR, ICON_DATABASE_GEAR);
		registerImage(ICON_DATABASE_GO, ICON_DATABASE_GO);
		registerImage(ICON_BUG, ICON_BUG);
		registerImage(ICON_TABLE_EDIT, ICON_TABLE_EDIT);
		registerImage(ICON_DATABASE_REFRESH, ICON_DATABASE_REFRESH);
		registerImage(ICON_FIND, ICON_FIND);
		registerImage(ICON_FIND_BY_KEY, ICON_FIND_BY_KEY);
		registerImage(ICON_FIND_KEY2, ICON_FIND_KEY2);
		registerImage(ICON_MIKE, ICON_MIKE);
	}
	
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
			declareImages();
		}
		return imageRegistry;
	}

	public static final void put(String key, Image image) {
		if (getImageRegistry().get(key) == null) {
			getImageRegistry().put(key, image);
		}
	}

	public static Image getImage(String key) {
		return getImageRegistry().get(key);
	}

	public static ImageDescriptor getImageDescriptor(String key) {
		return getImageRegistry().getDescriptor(key);
	}

	public static void remove(String key) {
		Image nil = null;
		getImageRegistry().put(key, nil);
	}

	private static final void registerImage(String key, String path) {
		InputStream is = ImageManager.class.getClass().getResourceAsStream(path);
		if (null != is) {
			Image img = new Image(null, is);
			imageRegistry.put(key, img);
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}